from selenium.webdriver.support.wait import WebDriverWait

from ConfigFile.Varconfig import testData_path
from Util.ExcelOperate import ExcelOperate


def find_element(wd,find_type,expression):
    '''
    查找单个元素
    :param driver:
    :param find_type:
    :param expression:
    :return:
    '''
    # element = wd.find_element(by=find_type,value=expression)
    # return element
    try:    #加上 智能等待时间
        element = WebDriverWait(wd,20).until(lambda wd:wd.find_element(by=find_type,value=expression))
        return element
    except Exception as e:
        raise e

def find_elements(wd,find_type,expression):
    '''
    查找多个元素
    :param driver:
    :param find_type:
    :param expression:
    :return:
        '''
    try:
        elements = WebDriverWait(wd,20).until(lambda wd:wd.find_elements(by=find_type,value=expression))
        return elements
    except Exception as e:
        raise  e

def assert_until(wd,assert_type,assert_value,*args):
    '''
    进行断言
    :param wd:
    :param assert_type:
    :param assert_value:
    :param args:
    :return:
    '''
    if assert_type == 'assert_title':
        assert assert_value in wd.title
    elif assert_type == 'assert_pagesource':
        assert assert_value in wd.page_source

def get_testdata():
    '''
    得到数据源 以[[xxx,xxx],[xxx,xxx]]
    :return:
    '''
    ex = ExcelOperate()
    ex.load_workbook(testData_path)
    ex.get_sheet_by_name('Sheet1')
    row_nums = ex.get_rows_nums()
    test_data = []

    for i in range(2,row_nums+1):
        row_values = ex.get_row_values(i)
        test_data.append(row_values)
    return test_data